package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.FactoryManagement;
import com.ruoyi.recycle.domain.Feedback;
import com.ruoyi.recycle.dto.FeedbackDTO;
import com.ruoyi.recycle.service.IFeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 意见反馈Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/feedback")
@Api(tags = "意见反馈")
public class FeedbackController extends BaseController {
    @Autowired
    private IFeedbackService feedbackService;

    /**
     * 查询意见反馈列表
     */
    @RequiresPermissions("recycle:feedback:list")
    @GetMapping("/list")
    public TableDataInfo list(Feedback feedback) {
        startPage();
        List<Feedback> list = feedbackService.selectFeedbackList(feedback);
        return getDataTable(list);
    }

    /**
     * 分页意见反馈列表
     */
    @RequiresPermissions("recycle:feedback:list")
    @PostMapping("/feedbackPageQuery")
    @ApiOperation("分页意见反馈列表")
    public IPage<Feedback> feedbackPageQuery(@RequestBody FeedbackDTO queryDTO) throws Exception{
        final Feedback factory = new Feedback();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<Feedback> wrapper = Wrappers.lambdaQuery(factory);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(Feedback::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(Feedback::getCreatedTime, simpleDateFormat.parse(old.format(queryDTO.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(Feedback::getCreatedTime);
        return feedbackService.page(new Page<Feedback>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出意见反馈列表
     */
    @RequiresPermissions("recycle:feedback:export")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出意见反馈列表")
    public void export(HttpServletResponse response, Feedback feedback) {
        List<Feedback> list = feedbackService.selectFeedbackList(feedback);
        ExcelUtil<Feedback> util = new ExcelUtil<>(Feedback.class);
        util.exportExcel(response, list, "意见反馈数据");
    }

    /**
     * 获取意见反馈详细信息
     */
    @RequiresPermissions("recycle:feedback:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取意见反馈详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(feedbackService.selectFeedbackById(id));
    }

    /**
     * 新增意见反馈
     */
    @RequiresPermissions("recycle:feedback:add")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增意见反馈")
    public AjaxResult add(@RequestBody Feedback feedback) {
        return toAjax(feedbackService.insertFeedback(feedback));
    }

    /**
     * 修改意见反馈
     */
    @RequiresPermissions("recycle:feedback:edit")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改意见反馈")
    public AjaxResult edit(@RequestBody Feedback feedback) {
        return toAjax(feedbackService.updateFeedback(feedback));
    }

    /**
     * 删除意见反馈
     */
    @RequiresPermissions("recycle:feedback:remove")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除意见反馈")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(feedbackService.deleteFeedbackByIds(ids));
    }
}
